/**
 * 
 */
package com.itboye.allshengmedical.sci.cmd.req;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicLongArray;

import com.itboye.allshengmedical.sci.DefaultSerialControl;

/**
 * 命令队列
 * 
 * @author 贝贝 346551990@qq.com
 * @date 2016-12-8
 */
public class CommandQueue {

	static CommandQueue instance = null;

	public static synchronized CommandQueue getInstance() {

		if (instance == null) {
			instance = new CommandQueue();
		}

		return instance;
	}

	private Queue<IReqCommand> waitcmdStack;
	public List<BaseCmd> sendedcmdStack;

	public List<BaseCmd> getSendedcmdStack() {
		return sendedcmdStack;
	}

	public void setSendedcmdStack(List<BaseCmd> sendedcmdStack) {
		this.sendedcmdStack = sendedcmdStack;
	}

	private CommandQueue() {
		this.waitcmdStack = new ArrayBlockingQueue<IReqCommand>(50);
		this.sendedcmdStack = new ArrayList<BaseCmd>(100);
	}

	public void pushCommand(IReqCommand cmd) {
		if (waitcmdStack.size() >= 50) {
			waitcmdStack.poll();
		}
		this.waitcmdStack.add(cmd);
	}

	public synchronized void pushSendedCommand(BaseCmd cmd) throws Exception {
		if (sendedcmdStack.size() >= 100) {
			return;
		}
		this.sendedcmdStack.add(cmd);
	}

	public synchronized void removeSentCommand(CmdType cmdType) {
		for (int i = 0; i < sendedcmdStack.size(); i++) {
			if (cmdType.equals(sendedcmdStack.get(i).getCmdType())) {
				sendedcmdStack.remove(i);
				break;
			}
		}
	}

	/**
	 * 取得命令，并从队列中移除这个命令
	 */
	public IReqCommand pollCommand() {
		return this.waitcmdStack.poll();
	}

}
